package com.lipaluma.field.validator.impl.string;

import java.lang.reflect.Field;
import java.util.List;

import com.lipaluma.annotations.validations.Validate;
import com.lipaluma.context.Context;
import com.lipaluma.field.validator.ValidationError;
import com.lipaluma.field.validator.Validator;
import com.lipaluma.xml.validator.XmlValidator;

public class StringNotEmptyValidator implements Validator {

	@Override
	public boolean init(Context context, Field field) {
		Validate annotation = field.getAnnotation(Validate.class);
		if(annotation == null || !annotation.notEmpty())
			return false;
		return true;
	}

	public boolean init(Context context, Field field, XmlValidator xmlValidator) {
		return true;
	}

	@Override
	public boolean validate(Object value, List<ValidationError> errors) {
		if(value.toString().isEmpty()) {
			errors.add(new ValidationError("The String value must not be empty"));
			return false;
		}
		return true;
	}

}
